package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;


    @CacheEvict(cacheNames = {"dishCache", "setmealCache"}, allEntries = true)
//新增商品的时候 把套餐和商品的缓存都清除掉
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto);
        dishService.saveWithFlavor(dto);
        return Result.success();

    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result page(DishPageQueryDTO dto) {
        log.info("菜品分页查询：{}", dto);
        PageResult result = dishService.pageQuery(dto);
        return Result.success(result);
    }

    @CacheEvict(cacheNames = {"dishCache", "setmealCache"}, allEntries = true)
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result batchDelete(@RequestParam List<Long> ids) {
        log.info("批量删除菜品：{}", ids);
        dishService.batchDelete(ids);
        return Result.success();

    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜品信息")
    public Result queryById(@PathVariable("id") Long id) {
        log.info("根据ID查询菜品信息：{}", id);
        DishVO vo = dishService.queryById(id);
        return Result.success(vo);

    }

    @CacheEvict(cacheNames = {"dishCache", "setmealCache"}, allEntries = true)
    @PutMapping
    @ApiOperation("编辑菜品")
    public Result update(@RequestBody DishDTO dto) {
        log.info("编辑菜品：{}", dto);
        dishService.update(dto);
        return Result.success();

    }

    /**
     * 使用路径参数 需要注意 /status/{status}
     *
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = {"dishCache", "setmealCache"}, allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售、停售")
    public Result enableOrDisable(@PathVariable("status") int status, Long id) {
        log.info("菜品起售、停售：{},{}", status, id);
        dishService.enableOrDisable(status, id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result page(Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);
        List<Dish> result = dishService.queryListByCategoryId(categoryId);
        return Result.success(result);
    }


}
